package com.welsy.newsmgt.command
{
	import com.adobe.cairngorm.commands.Command;
	import mx.rpc.IResponder;
	import com.adobe.cairngorm.control.CairngormEvent;
	import mx.controls.Alert;
	import mx.effects.AnimateProperty;
	import mx.collections.ArrayCollection;
	import com.welsy.newsmgt.business.GetAllEditingNewsDelegate;
	import com.welsy.newsmgt.event.GetAllEditingNewsEvent;
	import com.welsy.util.NamespaceLocator;
	import com.welsy.user.vo.NewsVO;
	import com.welsy.usermgt.vo.ClassVO;
	import com.welsy.newsmgt.model.NewsMgtModelLocator;
	
	public class GetAllEditingNewsCommand implements Command, IResponder
	{
		
		public function execute(event : CairngormEvent):void {
			var delegate : GetAllEditingNewsDelegate = new GetAllEditingNewsDelegate(this);
			var getAllEditingNewsEvent : GetAllEditingNewsEvent = GetAllEditingNewsEvent(event);

			delegate.getEditingNews(getAllEditingNewsEvent.authorId);
		}
		
		public function result(event : Object) :void {
			//parse the server result
			var aNS : Namespace = new Namespace(NamespaceLocator.NEWS_DATATYPES);			
			var base : XMLList = XMLList(event.result);

			var TAG_NEWSCOLLECTION : QName = new QName(aNS, "NewsCollection");			
			var TAG_NEWSTYPE : QName = new QName(aNS, "NewsType");

			var TAG_NEWSID : QName = new QName(aNS, "NewsId");
			var TAG_TITLE : QName = new QName(aNS, "Title");
			var TAG_DESCRIPTION : QName = new QName(aNS, "Description");
			var TAG_AUTHORID : QName = new QName(aNS, "AuthorId");
			var TAG_CREATIONDATE : QName = new QName(aNS, "CreationDate");
			
			//add the users
			var newsAdminTypeList : XMLList = base.descendants(TAG_NEWSCOLLECTION).children();			
			
			var newsList : ArrayCollection = new ArrayCollection;
			for each(var newsType : XML in newsAdminTypeList) {
				var buffer : NewsVO = new NewsVO;
				buffer.newsId = newsType.child(TAG_NEWSID);
				buffer.title = newsType.child(TAG_TITLE);
				buffer.authorId= newsType.child(TAG_AUTHORID);
				buffer.descr = newsType.child(TAG_DESCRIPTION);
				buffer.creationDate = newsType.child(TAG_CREATIONDATE);

				newsList.addItem(buffer);
			}
			NewsMgtModelLocator.getInstance().newsList = newsList;
		}
		
		public function fault( event : Object ):void {
			//TODO: report the error
			Alert.show("GetAllEditingNewsCommand.fault!");
		}
		
	}
}